﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using LeadThing.AbpZeroTemplate.WorkFlow_Processs;
using LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs;
using LeadThing.AbpZeroTemplate.WorkFlow_Runs;
using LeadThing.AbpZeroTemplate.WorkFlow_TableNames;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace LeadThing.AbpZeroTemplate.WorkFlows
{
    /// <summary>
    /// 流程表-名称信息业务管理
    /// </summary>
    public class WorkFlowManage : IDomainService
    {
        private readonly IRepository<WorkFlow, long> _workFlowRepository;
        private readonly IRepository<WorkFlow_Process, long> _workFlow_ProcessRepository;//流程节点配置表
        private readonly IRepository<WorkFlow_Run, long> _workFlow_RunRepository;//流程实例表
        private readonly IRepository<WorkFlow_Run_Process, long> _workFlow_Run_ProcessRepository;//实例节点流转明细

        /// <summary>
        /// 构造方法
        /// </summary>
        public WorkFlowManage(IRepository<WorkFlow, long> workFlowRepository,
            IRepository<WorkFlow_Process, long> workFlow_ProcessRepository,
            IRepository<WorkFlow_Run, long> workFlow_RunRepository,
            IRepository<WorkFlow_Run_Process, long> workFlow_Run_ProcessRepository)
        {
            _workFlowRepository = workFlowRepository;
            _workFlow_ProcessRepository = workFlow_ProcessRepository;
            _workFlow_RunRepository = workFlow_RunRepository;
            _workFlow_Run_ProcessRepository = workFlow_Run_ProcessRepository;
        }

        //TODO:编写领域业务代码

        /// <summary>
        /// 工作流通用方法
        /// </summary>
        /// <param name="detailID">关联业务实例ID</param>
        /// <param name="currentProcessID">当前节点ID</param>
        /// <param name="content">节点描述</param>
        public void AuditStart(int detailID, int currentProcessID, string content, int nextProcessID, int flowID)
        {
            #region 发起验证

            //var wf_RunList = _workFlow_RunRepository.GetAll().Where(p => p.DetailID == detailID && p.DelTag == "0").ToList();
            //if (wf_RunList.Count > 0)
            //{
            //    throw new UserFriendlyException("此业务已发起审核，不能重复发起！");
            //}

            #endregion

            WorkFlow_Run wf_RunModel = new WorkFlow_Run();
            var wf_RunList = _workFlow_RunRepository.GetAll().Where(p => p.DetailID == detailID && p.FlowID == flowID);
            var wf_ProcessModel = _workFlow_ProcessRepository.GetAll().Where(p => p.Id == nextProcessID).FirstOrDefault();//下一节点
            var wf_currentProcessModel = _workFlow_ProcessRepository.GetAll().Where(p => p.Id == currentProcessID).FirstOrDefault();//当前节点

            //修改
            if (wf_RunList.Count() > 0)
            {
                wf_RunModel = wf_RunList.FirstOrDefault();
                //获取当前流程是否是is_end和is_fail
                if (wf_ProcessModel.ProcessType == "is_end" || wf_ProcessModel.ProcessType == "is_fail")
                    wf_RunModel.Status = 1;//流转状态0流转中 1已办结
                else
                    wf_RunModel.Status = 0;//流转状态0流转中 1已办结
            }
            else//添加
            {
                wf_RunModel = new WorkFlow_Run()
                {
                    FlowID = flowID,
                    DetailID = detailID,
                    DelTag = true,
                    Status = 0,//流转状态0流转中 1已办结
                };
                wf_RunModel.Id = _workFlow_RunRepository.InsertAndGetId(wf_RunModel);
            }

            #region 写入实例节点流转明细的节点

            var _processType = wf_ProcessModel.ProcessType;
            if (wf_ProcessModel.ProcessType == "is_fail")
                _processType = "is_step";

            WorkFlow_Run_Process wf_Run_ProcessMoldel = new WorkFlow_Run_Process()
            {
                RunID = Convert.ToInt32(wf_RunModel.Id),//流程实例ID
                FlowID = flowID,
                ProessID = nextProcessID,
                IsBack = 0,                             //被退回的 0否(默认) 1是
                JS_Time = DateTime.Now,
                BL_Time = DateTime.Now,
                JJ_Time = DateTime.Now,
                Content = content,
                Status = 2,                             //状态 0为未接收（默认），1为办理中 ,2为已转交,3为已结束4为已打回
                PXM = 0,
                DelTag = true,
                ProcessType = _processType //步骤类型is_one发起 is_step正常步骤  is_end办结
            };

            _workFlow_Run_ProcessRepository.Insert(wf_Run_ProcessMoldel);

            if (wf_ProcessModel.ProcessType == "is_fail")
            {
                WorkFlow_Run_Process wf_Run_ProcessFailMoldel = new WorkFlow_Run_Process()
                {
                    RunID = Convert.ToInt32(wf_RunModel.Id),//流程实例ID
                    FlowID = flowID,
                    ProessID = nextProcessID,
                    IsBack = 0,                             //被退回的 0否(默认) 1是
                    JS_Time = DateTime.Now,
                    BL_Time = DateTime.Now,
                    JJ_Time = DateTime.Now,
                    Content = "确认转交其他渠道解决",
                    Status = 2,                             //状态 0为未接收（默认），1为办理中 ,2为已转交,3为已结束4为已打回
                    PXM = 0,
                    DelTag = true,
                    ProcessType = wf_ProcessModel.ProcessType //步骤类型is_one发起 is_step正常步骤  is_end办结
                };
                _workFlow_Run_ProcessRepository.Insert(wf_Run_ProcessFailMoldel);
            }

            #endregion
        }



        /// <summary>
        ///     初始化
        /// </summary>
        private void Init()
        {

        }
    }
}
